﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： WeChartUserRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-07-31
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using Dapper;
using YouWei.Utility;
using Dapper.Extensions;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///微信用户信息表 仓储层
    /// </summary>	
    public class WeChartUserRepository : BaseRepository<WeChartUser>
    {

        #region 构造

        public WeChartUserRepository() : base() { }

        public WeChartUserRepository(string connectionString) : base(connectionString) { }

        public WeChartUserRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 跟进OpenID查询
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public WeChartUser GetByOpenID(string openid)
        {
            string sql = "SELECT * FROM [WeChartUser] WITH(NOLOCK) WHERE OpenID=@OpenID";
            return FirstOrDefault(sql, new { OpenID = openid });
        }

        /// <summary>
        /// 跟进UnionID查询
        /// </summary>
        /// <param name="unionid"></param>
        /// <returns></returns>
        public List<WeChartUser> GetByUnionID(string unionid)
        {
            string sql = "SELECT * FROM [WeChartUser] WITH(NOLOCK) WHERE UnionID=@UnionID";
            return Query(sql, new { UnionID = unionid });
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<WeChartUser> Pages(int pageIndex,int pageSize, WechatUserSearchModel model)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM [WeChartUser] T1 WITH(NOLOCK) WHERE 1=1");
          
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.WeChartUserID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }


        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(WechatUserSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (!string.IsNullOrEmpty(model.OpenID))
            {
                sb.Append(" AND T1.OpenID=@OpenID");
                dparams.Add("OpenID", model.OpenID, System.Data.DbType.String);
            }

            if (!string.IsNullOrEmpty(model.UnionID))
            {
                sb.Append(" AND T1.UnionID=@UnionID");
                dparams.Add("UnionID", model.UnionID, System.Data.DbType.String);
            }

            if (!string.IsNullOrEmpty(model.NickName))
            {
                sb.Append(" AND T1.NickName=@NickName");
                dparams.Add("NickName", model.NickName, System.Data.DbType.String);
            }

            if (model.Sex > 0)
            {
                sb.Append(" AND T1.Sex=@Sex");
                dparams.Add("Sex", model.Sex, System.Data.DbType.Int32);
            }
            

            if (!string.IsNullOrEmpty(model.BCreateDate))
            {
                sb.Append(" AND T1.CreateDate>@BCreateDate");
                dparams.Add("BCreateDate", EConvert.ToDateTime(model.BCreateDate).Date, System.Data.DbType.DateTime);
            }

            if (!string.IsNullOrEmpty(model.ECreateDate))
            {
                sb.Append(" AND T1.CreateDate<@ECreateDate");
                dparams.Add("ECreateDate", EConvert.ToDateTime(model.BCreateDate).Date.AddDays(1), System.Data.DbType.DateTime);
            }

         
        }
	}
}